import type { PaginationProps } from '../types/pagination'
import type { BasicTableProps } from '../types/table'
import { computed, unref, ref, ComputedRef } from 'vue'
import { isBoolean } from '@/utils/is'
import { table } from '@/conf/component'

const { apiSetting, defaultPageSize, pageSizes } = table

export function usePagination(refProps: ComputedRef<BasicTableProps>) {
  const confRef = ref<PaginationProps>({})
  const show = ref(true)

  /**
   * 获取自定义翻页组件
   */
  const getPaginationInfo = computed((): PaginationProps | boolean => {
    const { pagination } = unref(refProps)
    return {
      pageSize: defaultPageSize,
      pageSizes: pageSizes,
      showSizePicker: true,
      showQuickJumper: true,
      ...(isBoolean(pagination) ? {} : pagination),
      ...unref(confRef)
    }
  })

  /**
   * 自定义翻页组件 & 默认配置值
   */
  const setPagination = (info: Partial<PaginationProps>) => {
    const paginationInfo = unref(getPaginationInfo)
    confRef.value = {
      ...(!isBoolean(paginationInfo) ? paginationInfo : {}),
      ...info
    }
  }

  return {
    getPaginationInfo,
    setPagination
  }
}
